package com.jiezheng.huinong.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.jiezheng.huinong.db.conf.IUserManageDAO;
import com.jiezheng.huinong.model.UserInfo;
import com.jiezheng.huinong.util.DbUtil;

public class UserManageDAOImpl implements IUserManageDAO {

	@Override
	public List<UserInfo> getUserInfo(Integer startRow, Integer rows) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		List<UserInfo> users = null;

		// 多值参数据的传递，包括起始行，与行数，实现分页
		Map<String, Integer> rowInfo = new HashMap<String, Integer>();
		rowInfo.put("start_row", startRow);
		rowInfo.put("rows", rows);

		try {
			users = session.selectList(
					"com.jiezheng.huinong.userManage.getUserInfoByPage",
					rowInfo);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return users;
	}

	@Override
	public void addUser(UserInfo user) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.userManage.addUser", user);
			session.insert("com.jiezheng.huinong.userManage.addUserInfo", user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public void deleteUser(UserInfo user) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.insert("com.jiezheng.huinong.userManage.deleteUser", user);
			session.insert("com.jiezheng.huinong.userManage.deleteUserInfo",
					user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public void modifyUser(UserInfo user) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			if (!(user.getPassword().isEmpty())) {
				session.update(
						"com.jiezheng.huinong.userManage.modifyUserAccount",
						user);
			}
			session.update("com.jiezheng.huinong.userManage.modifyUserInfo",
					user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public Integer getTotalUserNum() {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer totalNum = 0;
		try {
			totalNum = session
					.selectOne("com.jiezheng.huinong.userManage.getTotalUserNum");

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return totalNum;
	}

	@Override
	public void modifyUserAccount(UserInfo user) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		try {
			session.update("com.jiezheng.huinong.userManage.modifyUserAccount",
					user);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.commit();
			session.close();
		}
	}

	@Override
	public UserInfo getUserByLoginId(String loginId) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		UserInfo userIn = null;
		try {
			userIn = session
					.selectOne(
							"com.jiezheng.huinong.userManage.getUserByLoginId",
							loginId);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}
		return userIn;
	}

	@Override
	public Integer getMaxSn(String prefix) {
		SqlSession session = DbUtil.getSqlSessionFactory().openSession();
		Integer sn = null;
		try {
			sn = session.selectOne("com.jiezheng.huinong.userManage.getMaxSn",
					prefix);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			session.close();
		}

		// 未找到
		if (sn == null) {
			sn = 0;
		}

		return sn;
	}

}
